﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using ASPNETVN.PORTAL.Modules.EX.Components;
using System.Collections.Generic;

namespace ASPNETVN.PORTAL.Modules.EX
{
    public partial class Main : PortalModuleControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //DataSet ds = new DataSet();
            //ds.ReadXml("http://www.vietcombank.com.vn/ExchangeRates/ExrateXML.aspx");
            //GridView1.DataSource = ds.Tables[1].DefaultView;
            //GridView1.DataBind();

            // Update data to database.
            if (IsPostBack == false)
            {
                IList<Item> otherItems = new List<Item>();
                otherItems = Item.SelectCollectionDynamic(string.Format("(GETDATE() BETWEEN FromDate AND ToDate) AND (CurrencyCode <> 'USD')"), "CurrencyCode");
                if (otherItems.Count == 0)
                {
                    try
                    {
                        otherItems = ExchangeRate.GetTaxExchangeRate();
                        Item.InsertUpdateCollection(otherItems);
                    }
                    catch { }
                }
                gvExchangeRate.DataSource = otherItems;
                gvExchangeRate.DataBind();
                if (otherItems.Count > 0)
                {
                    lblOtherDate.Text = otherItems[0].FromDate.ToShortDateString() + " đến " + otherItems[0].ToDate.ToShortDateString();
                }

                IList<Item> usdItems = new List<Item>();

                usdItems = Item.SelectCollectionDynamic(string.Format("(YEAR(GETDATE()) = YEAR(FromDate) AND MONTH(GETDATE()) = MONTH(FromDate) AND DAY(GETDATE()) = DAY(FromDate)) AND (CurrencyCode = 'USD')"), "CurrencyCode");
                if (usdItems.Count == 0)
                {
                    try
                    {
                        usdItems = ExchangeRate.GetUSDExchangeRate();
                        Item.InsertUpdateCollection(usdItems);
                    }
                    catch { }


                }
                    
                gvExchangeRateOfUSD.DataSource = usdItems;
                gvExchangeRateOfUSD.DataBind();
                if (usdItems.Count > 0)
                {
                    lblUSDDate.Text = usdItems[0].FromDate.ToShortDateString();
                }
            }
        }
    }
}